﻿using System;
using System.Linq.Expressions;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;

namespace SharePoint.Orm.Caml {
    class Test {
        public int Age { get; set; }
    }
    [TestFixture]
    public class SPQueryTranslatorTest {
        [Test]
        public void WhereExpression_HasWhere() {
            Expression<Func<IQueryable<int>, IQueryable<int>>> whereExpr = i => i.Where(e => e == 0 && e != 2);
            SPQueryTranslator translator = new SPQueryTranslator();
            translator.Translate(whereExpr);
            Assert.AreEqual("<Where><And><Eq><FieldRef Name='e'/><Value Type='System.Int32'>0</Value></Eq><Neq><FieldRef Name='e'/><Value Type='System.Int32'>2</Value></Neq></And></Where>", translator.CamlString);
        }
        [Test]
        public void Translate_GreaterThanExpression_CorrectCaml() {
            Expression<Func<IQueryable<int>, IQueryable<int>>> whereExpr = i => i.Where(e => e > 2);
            SPQueryTranslator translator = new SPQueryTranslator();
            translator.Translate(whereExpr);
            Assert.AreEqual("<Where><Gt><FieldRef Name='e'/><Value Type='System.Int32'>2</Value></Gt></Where>",
                            translator.CamlString);
        }
        [Test]
        public void Translate_GreaterThanEqualExpression_CorrectCaml() {
            Expression<Func<IQueryable<int>, IQueryable<int>>> whereExpr = i => i.Where(e => e >= 2);
            SPQueryTranslator translator = new SPQueryTranslator();
            translator.Translate(whereExpr);
            Assert.AreEqual("<Where><Geq><FieldRef Name='e'/><Value Type='System.Int32'>2</Value></Geq></Where>",
                            translator.CamlString);
        }
        [Test]
        public void Translate_LessThanExpression_CorrectCaml() {
            Expression<Func<IQueryable<int>, IQueryable<int>>> whereExpr = i => i.Where(e => e < 2);
            SPQueryTranslator translator = new SPQueryTranslator();
            translator.Translate(whereExpr);
            Assert.AreEqual("<Where><Lt><FieldRef Name='e'/><Value Type='System.Int32'>2</Value></Lt></Where>",
                            translator.CamlString);
        }
        [Test]
        public void Translate_LessThanEqualExpression_CorrectCaml() {
            Expression<Func<IQueryable<int>, IQueryable<int>>> whereExpr = i => i.Where(e => e <= 2);
            SPQueryTranslator translator = new SPQueryTranslator();
            translator.Translate(whereExpr);
            Assert.AreEqual("<Where><Leq><FieldRef Name='e'/><Value Type='System.Int32'>2</Value></Leq></Where>",
                            translator.CamlString);
        }
        [Test]
        public void Translate_IsNullExpression_CorrectCaml() {
            Expression<Func<IQueryable<string>, IQueryable<string>>> whereExpr = i => i.Where(e => e == null);
            SPQueryTranslator translator = new SPQueryTranslator();
            translator.Translate(whereExpr);
            Assert.AreEqual("<Where><IsNull><FieldRef Name='e'/></IsNull></Where>",
                            translator.CamlString);
        }
        [Test]
        public void Translate_IsNotNullExpression_CorrectCaml() {
            Expression<Func<IQueryable<string>, IQueryable<string>>> whereExpr = i => i.Where(e => e != null);
            SPQueryTranslator translator = new SPQueryTranslator();
            translator.Translate(whereExpr);
            Assert.AreEqual("<Where><IsNotNull><FieldRef Name='e'/></IsNotNull></Where>",
                            translator.CamlString);
        }
        [Test]
        public void Translate_MemberAccess_CorrectCaml() {
            Expression<Func<IQueryable<Test>, IQueryable<Test>>> whereExpr = i => i.Where(e => e.Age == 2);
            SPQueryTranslator translator = new SPQueryTranslator();
            translator.Translate(whereExpr);
            Assert.AreEqual("<Where><Eq><FieldRef Name='Age'/><Value Type='System.Int32'>2</Value></Eq></Where>",
                            translator.CamlString);
        }
    }
}
